﻿using System.Windows.Controls;
using MassMailing.WPF.Settings;

namespace MassMailing.WPF
{
    /// <summary>
    /// Interaction logic for SMTPSettingUserControl.xaml
    /// </summary>
    public partial class SMTPSettingUserControl : UserControl
    {
        #region Properties

        /// <summary>
        /// The SMTP server text associated with this user control.
        /// </summary>
        public string SMTPServer
        {
            get
            {
                return txtSMTPServer.Text;
            }
            set
            {
                txtSMTPServer.Text = value;
            }
        }

        /// <summary>
        /// Is SSL Enabled for this user control.
        /// </summary>
        public bool? SSLEnabled
        {
            get
            {
                return chkEnableSSL.IsChecked;
            }
            set
            {
                chkEnableSSL.IsChecked = value ?? false;
            }
        }

        /// <summary>
        /// The port associated with this user control.
        /// </summary>
        public int? Port
        {
            get
            {
                int value;
                bool parsed = int.TryParse(txtPort.Text, out value);
                if (!parsed)
                {
                    return null;
                }

                return value;
            }
            set
            {
                txtPort.Text = value.HasValue ? value.ToString() : string.Empty;
            }
        }

        /// <summary>
        /// The maximum sent emails with this user control.
        /// </summary>
        public int? MaximumSentEmails
        {
            get
            {
                int value;
                bool parsed = int.TryParse(txtMaximumEmailsSent.Text, out value);
                if (!parsed)
                {
                    return null;
                }

                return value;
            }
            set
            {
                txtMaximumEmailsSent.Text = value.HasValue ? value.ToString() : string.Empty;
            }
        }

        /// <summary>
        /// The number of emails that should be sent on each burst.
        /// </summary>
        public int? BurstSize
        {
            get
            {
                int value;
                bool parsed = int.TryParse(txtBurstSize.Text, out value);
                if (!parsed)
                {
                    return null;
                }

                return value;
            }
            set
            {
                txtBurstSize.Text = value.HasValue ? value.ToString() : string.Empty;
            }
        }

        /// <summary>
        /// The delay in seconds between each burst.
        /// </summary>
        public int? BurstDelay
        {
            get
            {
                int value;
                bool parsed = int.TryParse(txtBurstDelay.Text, out value);
                if (!parsed)
                {
                    return null;
                }

                return value;
            }
            set
            {
                txtBurstDelay.Text = value.HasValue ? value.ToString() : string.Empty;
            }
        }

        /// <summary>
        /// The email address associated with this user control.
        /// </summary>
        public string EmailAddress
        {
            get
            {
                return txtEmailAddress.Text;
            }
            set
            {
                txtEmailAddress.Text = value;
            }
        }

        /// <summary>
        /// The password associated with this user control.
        /// </summary>
        public string Password
        {
            get
            {
                return txtPassword.Text;
            }
            set
            {
                txtPassword.Text = value;
            }
        }

        /// <summary>
        /// Whether the user control has the settings.
        /// </summary>
        public bool ContainsSettings
        {
            get
            {
                return !string.IsNullOrWhiteSpace(SMTPServer);
            }
        }

        #endregion

        /// <summary>
        /// Default Constructor
        /// </summary>
        public SMTPSettingUserControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Constructor that uses smtp settings to set up the user control.
        /// </summary>
        /// <param name="smtpSetting">The settings to use for the user control.</param>
        public SMTPSettingUserControl(SMTPSetting smtpSetting)
        {
            InitializeComponent();

            SMTPServer = smtpSetting.SMTPServer;
            SSLEnabled = smtpSetting.SSLEnabled;
            Port = smtpSetting.Port;
            MaximumSentEmails = smtpSetting.MaximumSentEmails;
            BurstSize = smtpSetting.BurstSize;
            BurstDelay = smtpSetting.BurstDelay;
            EmailAddress = smtpSetting.EmailAddress;
            Password = smtpSetting.Password;
        }
    }
}
